package cn.neusoft.luofa603.yuejian.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import cn.neusoft.luofa603.yuejian.Bean.UserLogin;
import cn.neusoft.luofa603.yuejian.Model;
import cn.neusoft.luofa603.yuejian.R;
import cn.neusoft.luofa603.yuejian.databinding.ActivityLoginBinding;


public class LoginActivity extends AppCompatActivity {
//    private UserLogin loginResult;
    SharedPreferences su;

    ActivityLoginBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        binding.setListeners(new Listeners());

    }

    public class Listeners {
        public void onClickLogin(){
//
            if(binding.tel.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
            }else if (binding.pwd.getText().toString().equals("")){
                Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
            }else {
//                    savesu();

                Model.with(Model.userService.Login(binding.tel.getText().toString(), binding.pwd.getText().toString()))
                        .positive(new Model.Positive<UserLogin>() {
                            @Override
                            public void onPositive(UserLogin data) {
                                if (data.getUser_name() != null) {
                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                } else
                                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        })
                        .request();
        }
        }
        public void onClickRegister(){
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivityForResult(intent,1);
            intent.getExtras();

        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data ){
        super.onActivityResult(requestCode,requestCode,data);
        if (requestCode==1){
            if(resultCode==RESULT_OK){
                String phone = data.getStringExtra("phone");
                String pwd = data.getStringExtra("pwd");
                binding.tel.setText(phone);
                binding.pwd.setText(pwd);
            }
        }
    }
//    public void savesu(){
//        SharedPreferences.Editor editor = su.edit();
//        binding.tel.getText().toString();
//        editor.toString();
//        binding.pwd.getText().toString();
//        editor.toString();
//        editor.commit();
//    }
}
